package flexulous;

/*
 * FlexulousPlugin.java
 * part of the Flexulous plugin for the jEdit text editor
 * Copyright (C) 2001 Chris Hill
 * jgellene@nyc.rr.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: FlexulousPlugin.java 12504 2008-04-22 23:12:43Z ezust $
 */

import org.gjt.sp.jedit.EditPlugin;
import org.gjt.sp.util.Log;
import java.io.File;
import java.util.List;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.GUIUtilities;
import org.gjt.sp.jedit.PluginJAR;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import errorlist.*;

import flexulous.model.FlexulousModel;
import flexulous.util.ClassPathHacker;
import flexulous.util.FlexulousCompilerHelper;
import flexulous.view.*;
import flexulous.model.*;
/**
 * The Flexulous plugin
 * 
 * @author Chris Hill
 */
public class FlexulousPlugin extends EditPlugin {
	public static final String NAME = "flexulous";
	public static final String OPTION_PREFIX = "options.flexulous.";
	
	private static FlexulousModel model;	
	/**
	 * Returns the value of model.
	 */
	
	public static FlexulousModel getModel()
	{
		return model;
	}

    private static DefaultErrorSource _errorSource;

    private static FlexulousPlugin instance;
    
    public static FlexulousPlugin getInstance()
    {
    	return instance;
    }
    
    public static DefaultErrorSource getErrorSource()
    {
        return _errorSource;
    }
	
	
	/**
	 * Sets the value of model.
	 * @param model The value to assign model.
	 */
	public static void setModel(FlexulousModel model)
	{
		FlexulousPlugin.model = model;
	}
	
	public void start()
	{
		File pluginDir = getPluginHome();
		Log.log(Log.WARNING, FlexulousPlugin.class, "PluginHome:"+pluginDir.getPath());
		if(!pluginDir.exists()){
			Log.log(Log.WARNING, FlexulousPlugin.class, "Plugin dir does not exist.");
			
			boolean madeDir = pluginDir.mkdirs();
			Log.log(Log.WARNING, FlexulousPlugin.class, "Made dir:"+madeDir);
		}else{
			Log.log(Log.WARNING, FlexulousPlugin.class, "Plugin dir exists.");
			
		}
		model = new FlexulousModel();
		model.setFilePath(pluginDir.getPath() + File.separator + "targets.xml");
		model.load();
		FlexulousPlugin.getModel().setGrowlLocation(jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX + "growllocation"));
		Log.log(Log.WARNING, FlexulousPlugin.class, "Model exists:"+getModel());
		
		Log.log(Log.WARNING, FlexulousPlugin.class, "loadCustomClassPath()");
        loadCustomClasspath();
		
        // initalize error source
        _errorSource = new DefaultErrorSource( NAME );
        ErrorSource.registerErrorSource(_errorSource);
        
        instance = this;

	}
	
	static void loadCustomClasspath()
    {
    		String sdkDirStr = jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX + "sdklocation");
    		//String mxmlcLocation = jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX + "sdklocation") + "/lib/mxmlc.jar";
    		File sdkDir = new File(sdkDirStr);
    		String sdkURI = "";
    		try{
    			sdkURI = sdkDir.toURI().toURL().toString()  + "/lib/flex-compiler-oem.jar";
    		}catch(MalformedURLException ex){
    			GUIUtilities.message(null, "flex-sdk-missing", null);
    		}
    		if(sdkDir.exists()){
    			try{
    				ClassPathHacker.addURL(new URL(sdkURI));
    				//ClassPathHacker.addURL(new URL("file://"+mxmlcLocation));
    				
    			}catch(Exception ex)
    			{
    				Log.log(Log.ERROR, null, "ClassPathError:"+ex);
    			}
    			/**
    			PluginJAR jar;
				
				jar = jEdit.getPluginJAR( sdkLocation );
				if ( jar == null ) {
					Log.log( Log.DEBUG, null, "- adding " + sdkLocation + " to jEdit plugins." );
					jEdit.addPluginJAR(sdkLocation);f
					jEdit.addPluginJAR(jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX + "sdklocation") + "/lib/swfutils.jar");
					//jEdit.addPluginJAR(jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX + "sdklocation") + "/lib/mxmlc.jar");
					
				}else{
					Log.log( Log.DEBUG, null, "- has been loaded before: " + sdkLocation );
				}
				**/
        	}else{
				GUIUtilities.message(null, "flex-sdk-missing", null);
				Log.log(Log.DEBUG, null, "sdk not found!");
			}
    }
    
    public void compileSelected()
    {
    	for (FlexulousCompilerHelper helper : model.getHelpers()) {
                if (helper.getTarget().isEnabled()) {
                    helper.compileTarget();
                }
        }
    }
        
    public void addTarget()
    {
    	new FlexulousTargetEditor(jEdit.getFirstView(), 
            new FlexulousCompilerHelper(new FlexulousTarget("","","")));
    }

    public void editTarget(FlexulousCompilerHelper helper)
    {
    	new FlexulousTargetEditor(jEdit.getFirstView(), helper);
    }
    
}
